local skel = fk.CreateSkill{
  name = "tea__lianxiu",
  tags = { Skill.Compulsory }
}
Fk:loadTranslationTable{
  ["tea__lianxiu"] = "炼修",
  [":tea__lianxiu"] = "锁定技，你的所有阶段均改为重铸你任意张手牌，然后你选择是否使用一张牌（不能与你本回合使用的上一张牌类型相同）。你每回合首次失去最后一张手牌后，" ..
  "你摸三张牌。回合结束时，若你的手牌数大于3，你将手牌调整至三张并回复1点体力。",
  ["#tea__lianxiu-invoke"] = "炼修：现在是 %arg ，你可以重铸任意张手牌并可以使用一张牌",
  ["#tea__lianxiu-use"] = "炼修：你可以使用一张牌",
  ["#tea__lianxiu-discard"] = "炼修：你须将手牌弃至三张，然后你回复1点体力",
}
---@type TrigSkelSpec<PhaseFunc>
local lianxiu = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.phase_end = true
    room:notifySkillInvoked(player, skel.name, "special")
    player:broadcastSkillInvoke(skel.name, player.phase - 1)
    local phases = { "", "phase_start", "phase_judge", "phase_draw", "phase_play", "phase_discard", "phase_finish" }
    local cids = {}
    if not player:isKongcheng() then
      cids = room:askToCards(player, {
        min_num = 1,
        max_num = 999,
        skill_name = skel.name,
        cancelable = true,
        prompt = "#tea__lianxiu-invoke:::" .. phases[player.phase],
        include_equip = false,
      })
    end
    if #cids > 0 then
      room:recastCard(cids, player, skel.name)
      if not player.dead then
        cids = table.filter(cids, function (id)
          return table.contains({ Card.DiscardPile, Card.DrawPile }, room:getCardArea(id))
        end)
        if #cids > 0 then
          local pattern = "."
          local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
          if turn_event then
            room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
              if e.data.from == player then
                local c_type = e.data.card.type
                local _types = { "^basic", "^trick", "^equip" }
                pattern = ".|.|.|.|.|" .. _types[c_type]
                return true
              end
            end, turn_event.id)
          end
          room:askToUseRealCard(player, {
            skill_name = skel.name,
            pattern = pattern,
            prompt = "#tea__lianxiu-use",
            extra_data = {
              bypass_times = true
            }
          })
        end
      end
    end
  end,
}
---@type TrigSkelSpec<MoveCardsFunc>
local lianxiu_trig = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name, true) and player:getMark(skel.name .. "-turn") == 0 then
      for _, move in ipairs(data) do
        if move.from == player then 
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              if player:isKongcheng() and player:hasSkill(skel.name) then
                player.room:setPlayerMark(player, skel.name .. "-turn", 1)
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player:drawCards(3, skel.name)
  end
}
---@type TrigSkelSpec<TurnFunc>
local lianxiu_discard = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and #player:getCardIds("h") > 3
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local num = #player:getCardIds("h") - 3
    room:askToDiscard(player, {
      min_num = num,
      max_num = num,
      cancelable = false,
      skill_name = skel.name,
      prompt = "#tea__lianxiu-discard"
    })
    if not player.dead and player:isWounded() then
      room:recover{
        who = player,
        num = 1,
        skillName = skel.name
      }
    end
  end
}
---@type TrigSkelSpec<TurnFunc>
local lianxiu_effect = {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:notifySkillInvoked(player, skel.name, "special")
  end
}
skel:addEffect(fk.EventPhaseStart, lianxiu)
skel:addEffect(fk.AfterCardsMove, lianxiu_trig)
skel:addEffect(fk.TurnEnd, lianxiu_discard)
skel:addEffect(fk.TurnStart, lianxiu_effect)
return skel